  // pages/orderUser/orderUser.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    level: "1",
    userPic:"",
    nickname:"",
    userInfo: [{
      userPic: "",
      nickname: "会飞的鱼",
      text: "落叶纷飞"
    }],
    isCheck: "",
    approvePhone: 0,

  },
  changeId: function() {
    wx.navigateTo({
      url: '../changeId/changeId',
    })
    wx.setStorageSync("type", null);
  },
  // 设置
  setting: function() {
    if (this.data.userPic == "") {
      wx.showToast({
        title: '请先点击登录',
        icon: "none"
      })
    } else {
    wx.navigateTo({
      url: '../receivingSetting/receivingSetting',
    })
    }
  },
  server: function() {
    wx.navigateTo({
      url: '../server/server',
    })
  },
  // 上传头像，昵称
  postHeader(header, name, userId) {
    wx.request({
      url: 'https://www.xiaozheng8.com/user/update',
      data: {
        userId: userId,
        headImg: header,
        nickName: name,
        mark: ""
      },
      method: 'post',
      success: function (res) {
        console.log(res);
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) { },
    })
  },
  // 我的关注
  attention: function() {
    if (this.data.userPic == "") {
      wx.showToast({
        title: '请先点击登录',
        icon: "none"
      })
    } else {
    wx.navigateTo({
      url: '../attention/attention',
    })
    }
  },
  address: function() {
    
    if (this.data.userPic == "") {
      wx.showToast({
        title: '请先点击登录',
        icon: "none"
      })
    } else {
    wx.request({
      url: 'https://www.xiaozheng8.com/sendUserAddr/findAll',
      data: {
        userId: wx.getStorageSync("userId"),
      },
      method: 'post',

      success: function(res) {
        console.log(res);

        wx.navigateTo({
          url: '../addressAdministration/addressAdministration',
        })

      }
    })
    }

  },
  // 用户微信登录
  getUserInfo(){
    var that=this;
    wx.getUserInfo({
      success: function (res) {
        console.log(res);
        wx.setStorage({
          key: 'info',
          data: res,
        })
        that.setData({
          nickname: res.userInfo.nickName,
          userPic: res.userInfo.avatarUrl

        })
        that.onLoad();
        that.onShow();
      },
      fail: function (res) {
        console.log(res);
      }
    })  
  },
  // 上传头像，昵称
  postHeader(header, name, userId) {
    wx.request({
      url: 'https://www.xiaozheng8.com/user/update',
      data: {
        userId: userId,
        headImg: header,
        nickName: name,
        mark: ""
      },
      method: 'post',
      success: function (res) {
        console.log(res);
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) { },
    })
  },
  // 获取个性签名
  mark: function() {
    var that = this;
    wx.request({
      url: 'https://www.xiaozheng8.com/receiveUser/findById',
      method: "post",
      data: {
        userId: wx.getStorageSync("userId")
      },
      success: function(res) {
        console.log(res);
        if(res.data==""){
          return false
        }
        that.setData({
          text: res.data.data.mark
        })
        console.log(that.data.text);
      },fail(err){
        console.log(err);
      }
    })
  },

  aboutUser: function() {
    wx.navigateTo({
      url: '../aboutUs/aboutUs',
    })
  },
  helpCenter: function() {
    wx.navigateTo({
      url: '../help/help',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  // 发单方认证
  companyInfo() {
    if (this.data.userPic == "") {
      wx.showToast({
        title: '请先点击登录',
        icon: "none"
      })
    } else {
    wx.navigateTo({
      url: '../companyInfo/companyInfo',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
    }
  },
  // 绑定手机
  bindPhone() {
    wx.navigateTo({
      url: '../bind/bind',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.getStorage({
      key: 'info',
      success: function(res) {
        console.log(res.data.userInfo);
        that.setData({
          nickname:res.data.userInfo.nickName,
          userPic:res.data.userInfo.avatarUrl

        })
        console.log(res.data.encryptedData);
        console.log(res.data.iv);
       wx.request({
         url: 'https://www.xiaozheng8.com/user/login',
         data:{
           code: app.globalData.code,
           iv:res.data.iv,
           encryptedData:res.data.encryptedData
         },
         method:"post",
         success(req){
           console.log(req)
           wx.setStorageSync("userId", req.data.data.userId);
           that.postHeader(that.data.userPic, that.data.nickname, req.data.data.userId);
           app.globalData.myUserInfo=true;
           
           that.onShow();
         },
         fail(req){
           console.log(req)
         }
       })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示 
   */
  onShow: function() {
    this.mark();
    app.request();
    // var num = wx.getStorageSync("tabBarNum");
    // if (num == 0) {
    //   wx.removeTabBarBadge({
    //     index: 1,
    //   });
    // } else {
    //   wx.setTabBarBadge({
    //     index: 1,
    //     text: "" + num + "",
    //   })
    // }
    // wx.setTabBarBadge({
    //   index: 1,
    //   text: "" + num + "",
    // })
    var that = this;
    wx.request({
      url: 'https://www.xiaozheng8.com/user/send/findById',
      data: {
        userId: wx.getStorageSync("userId"),
      },
      method: 'post',
      success: function(res) {
        if (res.data.data == undefined) {
          return false
        }
        console.log(res);
        that.setData({
          isCheck: res.data.data.isCheck
        })
      },
      fail: function(res) {
        console.log(res);

      },
      complete: function(res) {},
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    // wx.reLaunch({
    //   url: '../order/order'
    // })

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})